<?php
namespace app\api\controller;
use think\Db;
use think\Config;
use think\Request;
use app\api\Model;
use app\common\controller\User as commonUser;

class Index extends commonUser
{
   public function index(){
      $get_data=Db::table('categroy_menu')->select();
      return  json_encode($get_data);
   }

   public function find_menu()
   {
      $path = Db::table('categroy_menu')->where("id<>1")->order('id')->select();  
      return json_encode($path) ;
   }

   public function del_assets()
   {
      $where['id'] = $_GET['image_id'];
      $path = Db::table('categroy_image')->where($where)->delete();  
      return json_encode($path);

   }

   public function find_menupath()
   {
      $where['id']= $_GET['id'] ;
      $path = Db::table('categroy_menu')->where($where)->field('path')->find();  
      $get_id = explode('-',$path['path']); 
      $get =array_merge($get_id , array($where['id']));
      $path_name = Db::table('categroy_menu')->where('id' ,'in',$get)->field('name')->select();  
      return json_encode($path_name);
   }
   
  
   public function update_menu($status=0)
   {
      $where['id'] = $_GET['id'] ;
      $wherepid['pid'] = $_GET['id'] ;
      $whereimage['menu_id'] = $_GET['id'] ;
      $path=Db::table('categroy_image')->where($whereimage)->select();
      $piccount = Db::table('categroy_menu')->where($wherepid)->select();
      if(count($path)>0 ||count($piccount)>0){
            $path = 0;
      }else{
         if($status){
            $wherename['name'] = $_GET['name'] ;
            $path = Db::table('categroy_menu')->where($where)->update($wherename);  
          }
          $path = 1 ;
      }
      return json_encode($path) ;
   } 
     
   public function del_menu($status=0)
   {
      $where['id'] = $_GET['id'] ;
      $wherepid['pid'] = $_GET['id'] ;
      $whereimage['menu_id'] = $_GET['id'] ;
      $path=Db::table('categroy_image')->where($whereimage)->select();
      $piccount = Db::table('categroy_menu')->where($wherepid)->select();
     # return json_encode($path);
      if(count($path)>0 ||count($piccount)>0){
            $path = 0;
      }else{
         if($status){
            $path = Db::table('categroy_menu')->where($where)->delete();  
          }
          $path = 1 ;
      }
      return json_encode($path) ;
   }

   public function find_assets()
   {
      $where =input('param.');
      $get_data=Db::table('categroy_image')->where($where)->select();  
      return json_encode($get_data) ;
   }

   public function search_assets()
  {     
    $english_name = isset($_GET['english_name'])?$_GET['english_name']:'' ;
    $standard_name =  isset($_GET['standard_name'])?$_GET['standard_name']:'' ;
    $description =  isset($_GET['description'])?$_GET['description']:'' ;
    if ($english_name){
       $getdata = Db::table('categroy_image')->whereor('english_name','like', "%$english_name%")->select(); //
    }
    else if ($standard_name){
       $getdata = Db::table('categroy_image')->whereor('standard_name','like', "%$standard_name%")->select(); //
    }
    else{
       $getdata = Db::table('categroy_image')->whereor('description','like', "%$description%")->select(); //
    }
    return json_encode($getdata);
  }

   public function add_asset()
   {
      $get_data =input('param.');
      if(!isset($get_data['menu_id'])){
        return false ;
      }
      else{
        $insertdatabase = Db::table('categroy_image')->insert($get_data);
        if($insertdatabase){
            return json_encode("success") ;
        }
      }      
   }

   public function update_assets()
   {
      $get_data =input('param.');
      if(!isset($get_data['id'])){
        return false ;
      }
      else{
        $insertdatabase = Db::table('categroy_image')->update($get_data);
        if($insertdatabase){
            return json_encode("success") ;
        }
      }  
   }

   public function add_menu()
   {  
      $pid = isset($_GET['pid'])?$_GET['pid']:0;
      if(empty($_GET['name']))
      {
        return json_encode('id is null');
      }else{
        $name = $_GET['name'] ; 
      } 
      $path = Db::table('categroy_menu')->field('path,id')->where("id = $pid")->find();
      if(empty($path)){
          $path = '0';
      }
      $npath = $path['path'] . '-' . $path['id'];
      $data = ['pid' => $pid,'path' => $npath,'name'=>$name];
      $result = Db::table('categroy_menu')->insert($data);

      if($result)
      {
        $getid = Db::table('categroy_menu')->getLastInsID();
        return json_encode($getid) ;
      }else{
        return json_encode('error') ;
      }

   } 


}